﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace Html.Tests
{
	[TestFixture]
	public class AttributeTests
	{
		[Test]
		public void VerifyDoubleEquals()
		{
			string attlist = "Configuration='something'=='nothing'";
			Attribute attribute = new Attribute(attlist);
			Assert.AreEqual("'something'=='nothing'", attribute.Value);
			Element p = new Element("p", "Configuration='something'=='nothing';id=what;border=55;");
			Assert.AreEqual(3, p.Attributes.Count);
			Assert.AreEqual("'something'=='nothing'", p.Attributes[0].Value);
		}
		[Test]
		public void NewAttribute()
		{
			Attribute attr = new Attribute();
			attr.Name = "border";
			attr.Value = "2px";
			Element p = new Element("p");
			p.AddAttribute(attr);
			Assert.AreEqual("<p border=\"2px\"></p>", p.ToString());
		}
		[Test]
		[ExpectedException(typeof(ArgumentException))]
		public void AttributeCtorThrowsArgExceptionIfNullArgs()
		{
			new Attribute(null);
		}
		[Test]
		public void CanUpdateAttributesThroughIndexersAndNotThrowException()
		{
			Element span = new Element("span", "id=anja");
			span["id"] = "chris";
			Assert.AreEqual("<span id=\"chris\"></span>", span.ToString());
		}
		[Test]
		public void CanAddAttributesThroughIndex()
		{
			Element span = new Element("span");
			span["id"] = "hello";
			Assert.AreEqual("hello", span["id"]);
		}
		[Test]
		[ExpectedException(typeof(InvalidOperationException))]
		public void AddingStyleAsAttributeThrowsInvalidOperationException()
		{
			new Element("span").AddAttribute("style=border:0;");
		}

		[Test]
		[ExpectedException(typeof(InvalidOperationException))]
		public void AddingCssClassAsAttributeThrowsInvalidOperationException()
		{
			new Element("span").AddAttribute("class=chris");
		}
		[Test]
		[ExpectedException(typeof(DuplicateAttributeException))]
		public void SameAttributeAddedMultipleTimesLastOneInWins()
		{
			Element span = new Element("span");
			span.AddAttribute("width=50%");
			span.AddAttribute("width=75%");
		}
		[Test]
		public void ElementAddMethodAddsMultipleAttributes()
		{
			string expected = "<table id=\"test\" border=\"0\"></table>";
			string actual = new Element("table").AddAttribute("id=test;border=0");
			Assert.AreEqual(expected, actual);
		}
	}
}
